﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Structure.Interfaces;
using Structure.Objects;
using System.Data;
using Command;

namespace Application.Controls
{
    public partial class TaskForm : System.Web.UI.UserControl
    {
        public event Action OnAdd;
        private iCommand _command = BuildCommand.Build();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataTable dt = _command.ProjectList();
                ddlNewTaskProject.DataSource = dt;
                ddlNewTaskProject.DataTextField = "Наименование";
                ddlNewTaskProject.DataValueField = "#";
                ddlNewTaskProject.DataBind();
                DataTable dt1 = _command.UserList();
                ddlNewTaskExecuter.DataSource = dt1;
                ddlNewTaskExecuter.DataTextField = "Псевдоним";
                ddlNewTaskExecuter.DataValueField = "#";
                ddlNewTaskExecuter.DataBind();

                DataTable dt2 = _command.StatusList();
                ddlNewTaskStatus.DataSource = dt2;
                ddlNewTaskStatus.DataTextField = "Наименование";
                ddlNewTaskStatus.DataValueField = "#";
                ddlNewTaskStatus.DataBind();
            }
        }

        protected void btAddTask_Click(object sender, EventArgs e)
        {
            int status = 0;
            Task t = new Task(NewTaskName.Text, Convert.ToInt32(ddlNewTaskProject.SelectedValue), NewTaskDescription.Text, status, NewTaskStartDate.Text, NewTaskEndDate.Text, Convert.ToInt32(ddlNewTaskExecuter.SelectedValue));
           
            if (String.IsNullOrEmpty(TaskId.Text))
                _command.AddTask(t);
            else
                _command.UpdateTask(TaskId.Text, t);

            TaskFormDiv.Visible = false;
            NewTaskName.Text = "";
            NewTaskDescription.Text = "";
            ddlNewTaskProject.SelectedValue = null;
            NewTaskStartDate.Text = "";
            NewTaskEndDate.Text = "";
            ddlNewTaskExecuter.SelectedValue = null;
            ddlNewTaskStatus.SelectedValue = null;

            OnAdd();
        }

        protected void ProjectFormClose_Click(object sender, ImageClickEventArgs e)
        {
            TaskFormDiv.Visible = false;
        }

        protected void addTask_Click(object sender, EventArgs e)
        {
            TaskId.Text = String.Empty;
            TaskFormDiv.Visible = true;
        }

        public void Edit(string id)
        {
            Task t = _command.GetTask(id);

            NewTaskName.Text = t.Name;
            ddlNewTaskStatus.SelectedValue = t.Status.ToString();
            ddlNewTaskProject.SelectedValue = t.IdProject.ToString();
            ddlNewTaskExecuter.SelectedValue = t.IdProject.ToString();
            CalendarExtender1.SelectedDate = Convert.ToDateTime(t.StartDate);
            CalendarExtender2.SelectedDate = Convert.ToDateTime(t.EndDate);
            NewTaskDescription.Text = t.Description;
            //NewProjectStartDate.Text = p.StartDate;
            //     NewProjectEndDate.Text = p.EndDate;    
            TaskId.Text = id;

            TaskFormDiv.Visible = true;
        }
        
    }
}